package player;
import gui.*;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import p2pEngine.*;
import netPacks.message;

public class pinger extends Thread {
    
	public String mode = "toServer";
	boolean waitFlag = false;
    
	private pinger(){}
	
    private static volatile pinger instance = null;
    
    public static pinger getInstance() {
        if (instance == null){
            synchronized (pinger.class){
                    if (instance == null){
                        instance = new pinger();
                    }
        }}
        return instance;
    }
    
    public synchronized void run(){
    	message ping = new message();
		ping.setAction("ping");
		try {
			ping.setUserId(InetAddress.getLocalHost());
		} 
		catch (UnknownHostException e2) {
			e2.printStackTrace();
		}
    	while(!player.getInstance().pingerFailFlag){
    		if(waitFlag){
    			try {
					this.wait();
				} 
    			catch (InterruptedException e) {
					System.err.println("Cannot suspend pinger.\n"+e.getMessage());
    				e.printStackTrace();
				}
    		}
    		
    		try {
    				Thread.sleep(1000);
    			}
        		catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    			System.out.println("Sending ping check...");
        		if(this.mode == "toServer"){
        			try {
        				clientToServer.getInstance().oos.writeObject(ping);
        			} 
            		catch (IOException e) {
            			System.err.println("Cannot send ping request to server. \n"+e.getMessage());
            			clientToServer.getInstance().closeConnection();
            			System.err.println("Connection closed. Please reconnect.");
            			mainGui.getInstance().changeServerConButtonStat(true);
            			mainGui.getInstance().changeReadyButtonStat(false);
            			pinger.instance = null;
            			break;
        			}
            		catch (Exception e1){
            			System.err.println("Server connection lost.");
            			clientToServer.getInstance().closeConnection();
            			mainGui.getInstance().changeServerConButtonStat(true);
            			mainGui.getInstance().changeReadyButtonStat(false);
            			pinger.instance = null;
            			break;
            		}
        		}
        		else{
        			try {
        				p2pCon.getInstance().gameOos.writeObject(ping);
        			} 
            		catch (IOException e) {
            			System.err.println("Cannot send ping request to opponent.\n"+e.getMessage());
            			popupHandler.getInstance().getPopup("tryServerRecon");
            			pinger.instance = null;
            			break;
        			}
            		catch (Exception e1){
            			System.err.println("Cannot send ping request to opponent.\n"+e1.getMessage());
            			popupHandler.getInstance().getPopup("tryServerRecon");
            			pinger.instance = null;
            			break;
            		}
        		}
    		}
			System.err.println("connection lost.");
			mainGui.getInstance().changeServerConButtonStat(true);
			mainGui.getInstance().changeReadyButtonStat(false);
			pinger.instance = null;
			clientToServer.getInstance().closeConnection();
    		
    }
    
    public void setWaitFlag(boolean x){
    	this.waitFlag = x;
    }

    
}
